package com.hitrust.gs.action;

import java.util.ArrayList;
import java.util.HashMap;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.bean.GSOrganizationBean;
import com.hitrust.gs.business.GSBalanceProcessor;
import com.hitrust.gs.business.GSOrderDeptInfoProcessor;
import com.hitrust.gs.business.GSOrganizationProcessor;
import com.hitrust.gs.business.GSStocksProcesser;
import com.hitrust.gs.form.GSBalanceManagePayReceiveSaveForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.vo.GSBalanceManagePayRecvSaveVO;

public class GSBalanceManageRecPaySaveAction extends GSBaseAction {

	public String execute(GSBaseForm aForm) throws Exception {
		
		GSBalanceManagePayReceiveSaveForm tForm = (GSBalanceManagePayReceiveSaveForm)aForm;
		String message = "";
		ArrayList tList = new ArrayList();
		try{
			if(tForm.getRecvDate() != null && tForm.getRecvDate().length > 0){
				for(int i=0; i<tForm.getRecvDate().length;i++){
					if(tForm.getRecvDate()[i]!= null && !tForm.getRecvDate()[i].trim().equals("")){
						GSBalanceManagePayRecvSaveVO tVO = new GSBalanceManagePayRecvSaveVO();
						tVO.orgCategory = tForm.getOrgCategory();
						tVO.orgId = tForm.getOrgId();
						tVO.balancYM = tForm.getBalancYM();
						tVO.notes = tForm.getNotes();
						tVO.costType = tForm.getCostType()[i];
						tVO.balSum = tForm.getBalSum()[i];
						tVO.payType = tForm.getPayTypes()[i];
						tVO.principal = tForm.getPrincipal()[i];
						tVO.recvDate = DateUtil.formatDate(tForm.getRecvDate()[i]);
						if(tVO.orgCategory.equals(GSBaseConstants.BALANCE_ORDERDEPT_TYPE)){
							tVO.subOrgId = tForm.getSubOrgId();
							message = "生成应收数据操作";
							if(tVO.orgId.substring(2,4).equals("00")){
								tVO.orgLevel = GSBaseConstants.BALANC_WORKCORP_LEVEL;
								
							}else{
								tVO.orgLevel = GSBaseConstants.BALANCE_FACTORY_LEVEL;
								
							}
						}else{
							message = "生成应付数据操作";
						}
						tList.add(tVO);
					}
				}
			}
			GSBalanceProcessor tBalProc = new GSBalanceProcessor();
			//取得当前操作员可查询的订货单位
            GSOperatorBean tOperBean = (GSOperatorBean)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
            GSOrganizationProcessor orgpro=new GSOrganizationProcessor();
            ArrayList arr=orgpro.getOrgSub(tOperBean.getGs_oper_org_id());
            ArrayList ary=new ArrayList();
            for (int i = 0; i < arr.size(); i++) {
                String[] tOrderDept = new String[2];
                GSOrganizationBean ob=(GSOrganizationBean) arr.get(i);
                tOrderDept[0]=ob.getGs_org_id();
                tOrderDept[1]=ob.getGs_org_name();
                ary.add(tOrderDept);
            }
            aForm.getRequest().setAttribute("shiporg", ary);
	        //执行生成应收应付数据操作
			boolean tFlag = tBalProc.saveRecvPayMsg(tOperBean,tList);
			if(tFlag){
				tForm.getRequest().setAttribute("message",message + "成功!");
			}else{
				tForm.getRequest().setAttribute("message",message + "失败!");
			}
			GSStocksProcesser sp=new GSStocksProcesser();
			boolean bl=sp.updateorgprovstate();
			tForm.getRequest().setAttribute("requery","true");
		}catch(Exception e){
			tForm.getRequest().setAttribute("action","A180201.do");
			throw e;
		}
		return GSBaseConstants.FORWARD_SUCCESS;
	}

}
